# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

rm(list = ls())

library(tidyverse)
library(estimatr)
library(coefplot)

all_panels_long <- read_rds("data/all_panels_long.rds")

all_panels_long <-
  all_panels_long %>% 
  mutate(responded_w1 = !is.na(outcome_w1),
         responded_w2 = !is.na(outcome_w2),
         responded_w3 = !is.na(outcome_w3))


gg_df <- 
all_panels_long %>%
  group_by(fc, panel, panel_factor, rating) %>%
  do(tidy(lm_robust(formula(
    cbind(responded_w1, responded_w2, responded_w3) ~ treatment
  ), data = .))) %>% 
  filter(term != "(Intercept)", !is.nan(p.value)) %>% 
  mutate(term = str_remove(term, "treatment"),
         outcome = case_when(
           outcome == "responded_w1" ~ "Responded Wave 1",
           outcome == "responded_w2" ~ "Responded Wave 2",
           outcome == "responded_w3" ~ "Responded Wave 3"
         ))


pos = position_dodgev(height = 0.5)

g <-
  ggplot(gg_df) +
  aes(
    estimate,
    y = term,
    group = fc,
    shape = (p.value <= 0.05),
    color = (p.value <= 0.05)
  ) +
geom_vline(xintercept = 0, linetype = "dashed") +
  geom_point(position = pos) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high),
                 position = pos) +
  scale_color_manual(values = c(gray(0.5), gray(0.1))) +
  facet_grid(panel_factor ~ outcome) +
  theme_bw() +
  theme(strip.background = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none") +
  labs(x = "Estimated effect on response, relative to the misinformation condition")

g
# ggsave(filename = "plots/attrition.pdf",
#        g,
#        width = 6.5,
#        height = 9)


sum(gg_df$p.value <= 0.05)
sum(p.adjust(gg_df$p.value, method = "BH") <= 0.05)

